// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef NETDATA_WINDOWS_PLUGIN_H
#define NETDATA_WINDOWS_PLUGIN_H

#include "database/rrd.h"

#define PLUGIN_WINDOWS_NAME "windows.plugin"

// https://learn.microsoft.com/es-es/windows/win32/sysinfo/kernel-objects?redirectedfrom=MSDN
// 2^24
#define WINDOWS_MAX_KERNEL_OBJECT 16777216

#ifndef MEGA_FACTOR
#define MEGA_FACTOR (1048576)
#endif

#ifndef GIGA_FACTOR
#define GIGA_FACTOR (1073741824)
#endif

void win_plugin_main(void *ptr);

extern char windows_shared_buffer[8192];

// Windows API
int do_GetSystemUptime(int update_every, usec_t dt);
int do_GetSystemRAM(int update_every, usec_t dt);
int do_GetSystemCPU(int update_every, usec_t dt);
int do_GetPowerSupply(int update_every, usec_t dt);
int do_GetServicesStatus(int update_every, usec_t dt);
int do_GetHardwareInfo(int update_every, usec_t dt);
int do_GetSensors(int update_every, usec_t dt);

// Perflib
int do_PerflibStorage(int update_every, usec_t dt);
int do_PerflibNetwork(int update_every, usec_t dt);
int do_PerflibProcesses(int update_every, usec_t dt);
int do_PerflibProcessor(int update_every, usec_t dt);
int do_PerflibMemory(int update_every, usec_t dt);
int do_PerflibObjects(int update_every, usec_t dt);
int do_PerflibThermalZone(int update_every, usec_t dt);
int do_PerflibWebService(int update_every, usec_t dt);
int do_PerflibMSSQL(int update_every, usec_t dt);
int do_PerflibNetFramework(int update_every, usec_t dt);
int do_PerflibAD(int update_every, usec_t dt);
int do_PerflibADCS(int update_every, usec_t dt);
int do_PerflibADFS(int update_every, usec_t dt);
int do_PerflibHyperV(int update_every, usec_t dt);
int do_PerflibExchange(int update_every, usec_t dt __maybe_unused);
int do_PerflibNUMA(int update_every, usec_t dt __maybe_unused);
int do_PerflibASP(int update_every, usec_t dt __maybe_unused);

// Cleanup
void do_PerflibMSSQL_cleanup();
void do_GetHardwareInfo_cleanup();
void do_Sensors_cleanup();

enum MSSQL_REPLICATION_WARNINGS {
    MSSQL_REPLICATON_EXPIRATION = 1<<0,
    MSSQL_REPLICATON_LATENCY = 1<<1,
    MSSQL_REPLICATON_MERGEEXPIRATION = 1<<2,
    MSSQL_REPLICATON_MERGEFASTDURATION = 1<<3,
    MSSQL_REPLICATON_MERGELOWDURATION = 1<<4,
    MSSQL_REPLICATON_MERGEFASTRUNSPEED = 1<<5,
    MSSQL_REPLICATON_MERGELOWRUNSPEED = 1<<6
};

enum PERFLIB_PRIO {
    PRIO_WEBSITE_IIS_REQUESTS_RATE = 21000, // PRIO selected, because APPS is using 20YYY
    PRIO_WEBSITE_IIS_REQUESTS_BY_TYPE_RATE,
    PRIO_WEBSITE_IIS_TRAFFIC,
    PRIO_WEBSITE_IIS_FTP_FILE_TRANSFER_RATE,
    PRIO_WEBSITE_IIS_ACTIVE_CONNECTIONS_COUNT,
    PRIO_WEBSITE_IIS_CONNECTIONS_ATTEMP,
    PRIO_WEBSITE_IIS_USERS,
    PRIO_WEBSITE_IIS_ISAPI_EXT_REQUEST_COUNT,
    PRIO_WEBSITE_IIS_ISAPI_EXT_REQUEST_RATE,
    PRIO_WEBSITE_IIS_ERRORS_RATE,
    PRIO_WEBSITE_IIS_LOGON_ATTEMPTS,
    PRIO_WEBSITE_IIS_UPTIME,

    PRIO_IIS_APP_POOL_STATE,
    PRIO_IIS_APP_POOL_WORKER_PROCESSES_CURRENT,
    PRIO_IIS_APP_POOL_WORKER_PROCESSES_MAX,
    PRIO_IIS_APP_POOL_WORKER_PROCESS_RECENT_FAILURES,
    PRIO_IIS_APP_POOL_RECYCLES,
    PRIO_IIS_APP_POOL_TOTAL_WORKER_PROCESSES_CREATED,
    PRIO_IIS_APP_POOL_TOTAL_WORKER_PROCESS_FAILURES,
    PRIO_IIS_APP_POOL_TOTAL_WORKER_PROCESS_PING_FAILURES,
    PRIO_IIS_APP_POOL_TOTAL_WORKER_PROCESS_SHUTDOWN_FAILURES,
    PRIO_IIS_APP_POOL_TOTAL_WORKER_PROCESS_STARTUP_FAILURES,
    PRIO_IIS_APP_POOL_TOTAL_UPTIME,

    PRIO_W3SVC_W3WP_ACTIVE_THREADS,
    PRIO_W3SVC_W3WP_REQUESTS_TOTAL,
    PRIO_W3SVC_W3WP_REQUESTS_ACTIVE,
    PRIO_W3SVC_W3WP_FILE_CACHE_MEM_USAGE,
    PRIO_W3SVC_W3WP_FILE_CACHE_TOTAL,
    PRIO_W3SVC_W3WP_FILE_FLUSHED_TOTAL,
    PRIO_W3SVC_W3WP_URI_FLUSHED_TOTAL,
    PRIO_W3SVC_W3WP_URI_CACHED_TOTAL,
    PRIO_W3SVC_W3WP_METADATA_CACHED,
    PRIO_W3SVC_W3WP_METADATA_FLUSHED,
    PRIO_W3SVC_W3WP_OUTPUT_CACHE_ACTIVE_FLUSH,
    PRIO_W3SVC_W3WP_OUTPUT_CACHE_MEMORY_USAGE,
    PRIO_W3SVC_W3WP_OUTPUT_CACHE_FLUSHED_TOTAL,

    PRIO_MSSQL_USER_CONNECTIONS,
    PRIO_MSSQL_SESSIONS_CONNECTIONS,

    PRIO_MSSQL_DATABASE_TRANSACTIONS,
    PRIO_MSSQL_DATABASE_ACTIVE_TRANSACTIONS,
    PRIO_MSSQL_DATABASE_WRITE_TRANSACTIONS,
    PRIO_MSSQL_DATABASE_BACKUP_RESTORE_OPERATIONS,
    PRIO_MSSQL_DATABASE_LOG_FLUSHES,
    PRIO_MSSQL_DATABASE_LOG_FLUSHED,

    PRIO_MSSQL_DATABASE_LOCKWAIT_PER_SECOND,
    PRIO_MSSQL_DATABASE_DEADLOCKS_PER_SECOND,
    PRIO_MSSQL_DATABASE_LOCKS_TIMEOUT_PER_SECOND,
    PRIO_MSSQL_DATABASE_LOCK_REQUESTS_SEC,

    PRIO_MSSQL_DATABASE_READONLY,
    PRIO_MSSQL_DATABASE_STATE,
    PRIO_MSSQL_DATABASE_JOBS_STATUS,

    PRIO_MSSQL_DATABASE_DATA_FILE_SIZE,

    PRIO_MSSQL_STATS_BATCH_REQUEST,
    PRIO_MSSQL_STATS_COMPILATIONS,
    PRIO_MSSQL_STATS_RECOMPILATIONS,
    PRIO_MSSQL_STATS_AUTO_PARAMETRIZATION,
    PRIO_MSSQL_STATS_SAFE_AUTO_PARAMETRIZATION,

    PRIO_MSSQL_BLOCKED_PROCESSES,

    PRIO_MSSQL_BUFF_CACHE_HIT_RATIO,
    PRIO_MSSQL_BUFF_MAN_IOPS,
    PRIO_MSSQL_BUFF_CHECKPOINT_PAGES,
    PRIO_MSSQL_BUFF_METHODS_PAGE_SPLIT,
    PRIO_MSSQL_BUFF_PAGE_LIFE_EXPECTANCY,
    PRIO_MSSQL_BUFF_LAZY_WRITE,
    PRIO_MSSQL_BUFF_PAGE_LOOKUPS,

    PRIO_MSSQL_MEMMGR_CONNECTION_MEMORY_BYTES,
    PRIO_MSSQL_MEMMGR_TOTAL_SERVER,
    PRIO_MSSQL_MEMMGR_EXTERNAL_BENEFIT_OF_MEMORY,
    PRIO_MSSQL_MEMMGR_PENDING_MEMORY_GRANTS,

    PRIO_MSSQL_LOCKS_WAIT,
    PRIO_MSSQL_LOCKS_DEADLOCK_PER_RESOURCE,

    PRIO_MSSQL_WAITS_TOTAL,
    PRIO_MSSQL_RESOURCE_WAIT,
    PRIO_MSSQL_SIGNAL_WAIT,
    PRIO_MSSQL_MAX_WAIT_TIME,
    PRIO_MSSQL_WAITING_COUNT,

    PRIO_MSSQL_SQL_ERRORS,

    PRIO_MSSQL_REPLICATION_STATUS,
    PRIO_MSSQL_REPLICATION_WARNING,
    PRIO_MSSQL_REPLICATION_AVG_LATENCY,
    PRIO_MSSQL_REPLICATION_SUBSCRIPTION_COUNT,
    PRIO_MSSQL_REPLICATION_AGENT_RUNNING,
    PRIO_MSSQL_REPLICATION_SYNC_TIME,

    PRIO_NETFRAMEWORK_CLR_EXCEPTION_THROWN,
    PRIO_NETFRAMEWORK_CLR_EXCEPTION_FILTERS,
    PRIO_NETFRAMEWORK_CLR_EXCEPTION_FINALLYS,
    PRIO_NETFRAMEWORK_CLR_EXCEPTION_THROW_TO_CATCH_DEPTH,

    PRIO_NETFRAMEWORK_CLR_INTEROP_CCW,
    PRIO_NETFRAMEWORK_CLR_INTEROP_MARSHALLING,
    PRIO_NETFRAMEWORK_CLR_INTEROP_STUBS_CREATED,

    PRIO_NETFRAMEWORK_CLR_JIT_METHODS,
    PRIO_NETFRAMEWORK_CLR_JIT_TIME,
    PRIO_NETFRAMEWORK_CLR_JIT_STANDARD_FAILURES,
    PRIO_NETFRAMEWORK_CLR_JIT_IL_BYTES,

    PRIO_NETFRAMEWORK_CLR_LOADING_HEAP_SIZE,
    PRIO_NETFRAMEWORK_CLR_LOADING_APP_DOMAINS_LOADED,
    PRIO_NETFRAMEWORK_CLR_LOADING_APP_DOMAINS_UNLOADED,
    PRIO_NETFRAMEWORK_CLR_LOADING_ASSEMBLIES_LOADED,
    PRIO_NETFRAMEWORK_CLR_LOADING_CLASSES_LOADED,
    PRIO_NETFRAMEWORK_CLR_LOADING_CLASS_LOAD_FAILURE,

    PRIO_NETFRAMEWORK_CLR_REMOTING_CHANNELS,
    PRIO_NETFRAMEWORK_CLR_REMOTING_CONTEXT_BOUND_CLASSES_LOADED,
    PRIO_NETFRAMEWORK_CLR_REMOTING_CONTEXT_BOUND_OBJECTS,
    PRIO_NETFRAMEWORK_CLR_REMOTING_CONTEXTS_PROXIES,
    PRIO_NETFRAMEWORK_CLR_REMOTING_CONTEXTS,
    PRIO_NETFRAMEWORK_CLR_REMOTING_REMOTE_CALLS,

    PRIO_NETFRAMEWORK_CLR_SECURITY_LINK_TIME_CHECKS,
    PRIO_NETFRAMEWORK_CLR_SECURITY_RT_CHECKS_TIME,
    PRIO_NETFRAMEWORK_CLR_SECURITY_STACK_WALK_DEPTH,
    PRIO_NETFRAMEWORK_CLR_SECURITY_RUNTIME_CHECKS,

    PRIO_NETFRAMEWORK_CLR_LOCKS_AND_THREADS_QUEUE_LENGTH,
    PRIO_NETFRAMEWORK_CLR_LOCKS_AND_THREADS_LOGICAL_THREADS,
    PRIO_NETFRAMEWORK_CLR_LOCKS_AND_THREADS_CURRENT_PHYSICAL_THREADS,
    PRIO_NETFRAMEWORK_CLR_LOCKS_AND_THREADS_RECOGNIZED_THREADS,
    PRIO_NETFRAMEWORK_CLR_LOCKS_AND_THREADS_CONTENTIONS,

    PRIO_SERVICE_STATE,

    PRIO_AD_OPERATIONS_TOTAL,
    PRIO_AD_DIROPERATIONS_TOTAL,
    PRIO_AD_CACHE_LOOKUP_TOTAL,
    PRIO_AD_CACHE_HITS_TOTAL,
    PRIO_AD_REPLICATION_INTRASITE_COMPRESSED,
    PRIO_AD_REPLICATION_INTERSITE_COMPRESSED,
    PRIO_AD_SYNC_OBJECTS_REMAINING,
    PRIO_AD_REPLICATION_PROPERTY_UPDATED,
    PRIO_AD_REPLICATION_PROPERTY_FILTERED,
    PRIO_AD_SYNC_PENDING,
    PRIO_AD_SYNC_TOTAL,
    PRIO_AD_THREADS_IN_USE,
    PRIO_AD_BIND_TIME,
    PRIO_AD_BIND_TOTAL,
    PRIO_AD_LDAP_SEARCHES_TOTAL,
    PRIO_AD_AVG_REQUEST_LATENCY,
    PRIO_AD_OUTSTANDING_REQUEST,

    PRIO_ADCS_CERT_REQUESTS_TOTAL,
    PRIO_ADCS_CERT_REQUESTS_PROCESSING_TIME,
    PRIO_ADCS_CERT_RETRIVALS,
    PRIO_ADCS_CERT_FAILED_REQUESTS,
    PRIO_ADCS_CERT_ISSUED_REQUESTS,
    PRIO_ADCS_CERT_PENDING_REQUESTS,
    PRIO_ADCS_CERT_CHALLENGE_RESPONSES,
    PRIO_ADCS_CERT_RETRIEVAL_PROCESSING_TIME,
    PRIO_ADCS_CERT_REQ_CRYPTO_SIGNING_TIME,
    PRIO_ADCS_CERT_REQ_POLICY_MODULE_PROCESS_TIME,
    PRIO_ADCS_CERT_CHALLENGE_RESP_PROCESSING_TIME,
    PRIO_ADCS_CERT_SIGNED_CERTIFICATE_TIMESTAMP_LIST,
    PRIO_ADCS_CERT_SIGNED_CERTIFICATE_TIMESTAMP_PROC_TIME_LIST,

    PRIO_ADFS_LOGIN_CONNECTION_FAILURES_TOTAL,
    PRIO_ADFS_CERTIFICATE_AUTHENTICATION_TOTAL,
    PRIO_ADFS_DB_ARTIFACT_FAILURE_TOTAL,
    PRIO_ADFS_DB_ARTIFACT_QUERY_TYME_SECONDS_TOTAL,
    PRIO_ADFS_DB_CONFIG_FAILURE_TOTAL,
    PRIO_ADFS_DB_CONFIG_QUERY_TYME_SECONDS_TOTAL,
    PRIO_ADFS_DEVICE_AUTHENTICATIONS_TOTAL,
    PRIO_ADFS_EXTERNAL_AUTHENTICATION_TOTAL,
    PRIO_ADFS_FEDERATION_AUTHENTICATION_TOTAL,
    PRIO_ADFS_FEDERATION_REQUESTS_AUTHENTICATION_TOTAL,

    PRIO_ADFS_OAUTH_AUTHORIZED_REQUEST,
    PRIO_ADFS_OAUTH_CLIENT_AUTHORIZATION_REQUEST,
    PRIO_ADFS_OAUTH_CLIENT_CREDENTIAL_REQUEST,
    PRIO_ADFS_OAUTH_CLIENT_PRV_KEY_JWT_AUTH,
    PRIO_ADFS_OAUTH_CLIENT_SECRET_BASIC_AUTH,
    PRIO_ADFS_OAUTH_CLIENT_SECRET_POST_AUTH,
    PRIO_ADFS_OAUTH_CLIENT_WINDOWS_AUTH,
    PRIO_ADFS_OAUTH_TOKEN_REQUESTS_SUCCESS,

    PRIO_ADFS_PASSIVE_REQUESTS,
    PRIO_ADFS_PASSPORT_AUTHENTICATOR,
    PRIO_ADFS_PASSWORD_CHANGE_REQUEST,
    PRIO_ADFS_SAMLP_TOKEN_REQUESTS,
    PRIO_ADFS_SSO_AUTH,
    PRIO_ADFS_TRUST_TOKEN_SUCCESS,
    PRIO_ADFS_USER_PASS_AUTH,
    PRIO_ADFS_WSFED_TOKEN_REQUESTS,

    PRIO_EXCHANGE_QUEUE_ACTIVE_MAILBOX_DELIVERY,
    PRIO_EXCHANGE_QUEUE_EXTERNAL_ACTIVE_REMOTE_DELIVERY,
    PRIO_EXCHANGE_QUEUE_EXTERNAL_LARGEST_DELIVERY,
    PRIO_EXCHANGE_QUEUE_INTERNAL_ACTIVE_REMOTE_DELIVERY,
    PRIO_EXCHANGE_QUEUE_INTERNAL_LARGEST_DELIVERY,
    PRIO_EXCHANGE_QUEUE_RETRY_MAILBOX_DELIVERY,
    PRIO_EXCHANGE_QUEUE_UNREACHABLE,
    PRIO_EXCHANGE_QUEUE_POISON,
    PRIO_EXCHANGE_ACTIVE_SYNC_PING_CMDS_PENDING,
    PRIO_EXCHANGE_ACTIVE_SYNC_REQUESTS,
    PRIO_EXCHANGE_ACTIVE_SYNC_CMDS,
    PRIO_EXCHANGE_RPC_ACTIVE_USERS_COUNT,
    PRIO_EXCHANGE_RPC_AVG_LATENCY,
    PRIO_EXCHANGE_RPC_CONNECTION_COUNT,
    PRIO_EXCHANGE_RPC_OPERATIONS_TOTAL,
    PRIO_EXCHANGE_RPC_REQUESTS,
    PRIO_EXCHANGE_RPC_USER_COUNT,
    PRIO_EXCHANGE_OWA_REQUESTS_TOTAL,
    PRIO_EXCHANGE_OWA_UNIQUE_USERS,
    PRIO_EXCHANGE_WORKlOAD_ACTIVE_TASKS,
    PRIO_EXCHANGE_WORKlOAD_COMPLETE_TASKS,
    PRIO_EXCHANGE_WORKlOAD_QUEUE_TASKS,
    PRIO_EXCHANGE_WORKlOAD_YIELDED_TASKS,
    PRIO_EXCHANGE_WORKlOAD_ACTIVITY,
    PRIO_EXCHANGE_PROXY_AVG_AUTH_LATENCY,
    PRIO_EXCHANGE_PROXY_AVG_CAS_PROCESSING_LATENCY,
    PRIO_EXCHANGE_PROXY_MAILBOX_PROXY_FAILURE,
    PRIO_EXCHANGE_PROXY_SERVER_LOCATIOR_AVG_LATENCY,
    PRIO_EXCHANGE_PROXY_OUTSTANDING_PROXY_REQUESTS,
    PRIO_EXCHANGE_PROXY_PROXY_REQUESTS_TOTAL,
    PRIO_EXCHANGE_AUTO_DISCOVER_REQUESTS,
    PRIO_EXCHANGE_AUTO_AVAILABILITY_SERVICES,

    PRIO_ASPNET_APPLICATION_RESTART,
    PRIO_ASPNET_WORKER_PROCESS_RESTART,
    PRIO_ASPNET_APP_ANONYMOUS_REQUEST,
    PRIO_ASPNET_APP_COMPILATION_TOTALS,
    PRIO_ASPNET_ERRORS_DURING_PREPROCESSING,
    PRIO_ASPNET_ERRORS_DURING_COMPILATION,
    PRIO_ASPNET_ERRORS_DURING_EXECUTION,
    PRIO_ASPNET_ERRORS_UNHANDLED_EXECUTION_PER_SEC,
    PRIO_ASPNET_REQUESTS_BYTES_TOTAL,
    PRIO_ASPNET_REQUESTS_EXECUTING,
    PRIO_ASPNET_REQUESTS_FAILED,
    PRIO_ASPNET_REQUESTS_NOT_FOUND,
    PRIO_ASPNET_REQUESTS_IN_APPLICATION_QUEUE,
    PRIO_ASPNET_REQUESTS_NOT_AUTHORIZED,
    PRIO_ASPNET_REQUESTS_TIMEOUT,
    PRIO_ASPNET_REQUESTS_SUCCEEDED,
    PRIO_ASPNET_SESSIONS_ACTIVE,
    PRIO_ASPNET_SESSIONS_ABANDONED,
    PRIO_ASPNET_SESSIONS_TIMEDOUT,
    PRIO_ASPNET_TRANSACTIONS_ABORTED,
    PRIO_ASPNET_TRANSACTIONS_COMMITED,
    PRIO_ASPNET_TRANSACTIONS_PENDING,
    PRIO_ASPNET_TRANSACTIONS_PER_SEC,
    PRIO_ASPNET_EVENTS_RAISED_PER_SEC,
    PRIO_ASPNET_ERROR_EVENTS_RAISED_PER_SEC,
    PRIO_ASPNET_AUDIT_SUCCESS_EVENTS_RAISED,
    PRIO_ASPNET_AUDIT_FAILURES_EVENTS_RAISED,
    PRIO_ASPNET_MEMBERSHIP_AUTHENTICATION_SUCCESS,
    PRIO_ASPNET_MEMBERSHIP_AUTHENTICATION_FAILURE,
    PRIO_ASPNET_FORM_AUTHENTICATION_SUCCESS,
    PRIO_ASPNET_FORM_AUTHENTICATION_FAILURE
};

#endif //NETDATA_WINDOWS_PLUGIN_H
